#include "lib.h"

#include "CDBInterface.h"
#include "odbc.h"
#include "opts.h"
#include "CGame.h"
#include "progsql.h"
#include "CaribbeanDefs.h"
#include "CaribbeanCommon.h"
#include "memory.h"


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     CDBInterface::CDBInterface(ProgressiveTrackSQL *pTracker, CInfo *pInfo)
///
/// @param  pTracker    (in) provides connections to the database and has other useful abilities
/// @param  pInfo       (in) info about the current player session
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
CDBInterface::CDBInterface(ProgressiveTrackSQL *pTracker, CInfo *pInfo)
{
    m_pTracker  = pTracker;
    m_pInfo     = pInfo;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     CDBInterface::trackGetBalance( int32 *balance )
///
///  Get the current user's balance
///
/// @param  balance     (out) the current player's balance
/// 
/// @return bool8       true on success, false otherwise
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
bool8 CDBInterface::trackGetBalance( int32 *balance )
{    
    int32 result = TrackSQL::ExecFailed;
    int32 comp;
    *balance = 0;

    if (m_pTracker && m_pInfo)
    {
        m_pTracker->lock();

        result = m_pTracker->getBalance(ServerOpts::casinoId, m_pInfo->pid, 
                                        m_pInfo->forMoney, balance, &comp);

        m_pTracker->unlock();
    }

    return (bool8)(result == TrackSQL::NoError);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     CDBInterface::trackAdjustBalance(int32 Amount)
///
/// Change the current user's balance.
///
/// @param  Amount      (in)  the amount to add or subtract from the balance
/// 
/// @return int32       the new adjusted balance
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
int32 CDBInterface::trackAdjustBalance(int32 Amount)
{
    int32 result = TrackSQL::ExecFailed;
    int32 bal=0, comp=0;

    if (m_pTracker && m_pInfo)
    {
        m_pTracker->lock();

        result = m_pTracker->adjustBalance(ServerOpts::casinoId, m_pInfo->pid, 
                                           m_pInfo->forMoney, Amount, &bal, &comp);

        m_pTracker->unlock();
    }

    if (result == TrackSQL::NoError)
    {

        return bal;
    }

    return 0;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     CDBInterface::trackGetPlayerLimits(CaribbeanMachineId eMachineId, int32* minBet, 
///                                            int32* maxBet, int32* compFactor)
///
/// Get min/max betting limit info for the indicated game.
///
/// @param  eMachineId      (in)    The machine id of the game that we want the settings for.
/// @param  minBet          (out)   lower limit for bets on the indicated game
/// @param  maxBet          (out)   upper limit for bets on the indicated game
/// @param  compFactor      (out)  
/// 
/// @return bool8           true on success, false otherwise.
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
bool8 CDBInterface::trackGetPlayerLimits(CaribbeanGameId eGameId, int32* minBet, 
                                         int32* maxBet, int32* compFactor)
{
    int32 result = TrackSQL::ExecFailed;

    if (m_pTracker && m_pInfo)
    {
        m_pTracker->lock();

        result = m_pTracker->getPlayerLimits(m_pInfo->pid, eGameId, 0,
                                             minBet, maxBet, compFactor);

        m_pTracker->unlock();
    }

    return (bool8)(result == TrackSQL::NoError);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     CDBInterface::trackValidateLogin()
///
/// Complete the login process for the current player (using the data in the CInfo object).
/// 
/// @return     the return code of validateLogin
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
int32 CDBInterface::trackValidateLogin()
{
	int32 result = TrackSQL::ExecFailed;

	if (m_pTracker)
	{
		m_pTracker->lock();

		result = m_pTracker->validateLogin(m_pInfo->login, m_pInfo->sessionId, m_pInfo->pid);

		m_pTracker->unlock();
	}

	return result;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     CDBInterface::trackValidateSession()
///
/// Verify the player's session is still active
/// 
/// @return     the return code of validateSession
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
int32 CDBInterface::trackValidateSession()
{
	int32 result = TrackSQL::ExecFailed;

	if (m_pTracker)
	{
		m_pTracker->lock();

		result = m_pTracker->validateSession(m_pInfo->pid, m_pInfo->sessionId);

		m_pTracker->unlock();
	}

	return result;
}